// 拦截器操作

// import type { Result } from '@/api/model'
import type {
  AxiosError,
  AxiosInstance,
  AxiosRequestConfig,
  AxiosResponse,
  InternalAxiosRequestConfig
} from 'axios'
import { message } from 'ant-design-vue'
// import { useRouter } from 'vue-router'
import { useInfoStore } from '@/store'
// const router = useRouter()
import router from '@/router/index'
import { throttle } from 'lodash'

const showMessage = () => {
  void router.push('/login')
  void message.error('登录已过期，需重新登录')
  console.log('登录已过期，需重新登录')
}
// 立即调用一次,节流结束后不调用
const showMessageThrottle = throttle(showMessage, 5000, {
  leading: true,
  trailing: false
})

// 不需要经过处理的接口白名单  'auth/token/logout'
const whiteList: string | string[] = [
  'auth/oauth/token',
  '/system/platform/logo'
]

// 请求拦截器
// const refreshServices: Array<() => void> = []

// 处理请求头，一般比如在header加token
export const handlerRequest = async (config: AxiosRequestConfig) => {
  if (whiteList.includes(config.url as string))
    return config as InternalAxiosRequestConfig<any>
  // 判断是否是刷新token接口
  // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
  const store = useInfoStore()
  const token = store.token
  // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
  if (token) {
    // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
    config.headers!.Authorization = `Bearer ${token}`
  }
  return config as InternalAxiosRequestConfig<any>
}

// 根据情况处理返回数据
// eslint-disable-next-line @typescript-eslint/no-unused-vars
export const handleResponse = (
  res: AxiosResponse,
  instance?: AxiosInstance
) => {
  // todo 在某些情况下instance可能会有用处
  const data = res.data
  if (res.config.responseType === 'blob') return res
  // console.log('data: ', data)
  // // 根据不同的code判断接下来的操作
  // console.log('data.message.code: ', data.message.code)
  let flag = true
  switch (data.message.code) {
    case 200:
      flag = false
      break
    case 0:
      flag = false
      break
    case 500:
      void message.error(data.message.message)
      break
    case 1005:
      void message.error(data.message.message)
      break
    case 1701:
      void message.error(data.message.message)
      break
    case 10166:
    case 401:
      // 处理密码超过90天了没更换跳转到修改密码页面
      showMessageThrottle()
      break
    case -1:
    case -2:
    case 10133:
    case 10157:
    case 10307:
      // 10133: token过期  10157: 账号被顶掉
      void router.push('/login')
      break
    default:
      void message.error(data.message.message)
      break
  }
  if (!flag) {
    // AxiosResponse data
    res.data = res.data.data
    return res
  }
}

export const handleResponseErr = async (err: AxiosError) => {
  const { response } = err
  if (response != null) {
    // 根据状态码判定接口错误类型
    // switch (response.status) {
    //   case 404:
    //     return Promise.reject(response.statusText);
    // }
    void message.error(response.status.toString() + ' ' + response.statusText)
    return await Promise.reject(response.statusText)
  }
  return await Promise.reject(err.message)
}
